package util
{
	import mx.containers.TabNavigator;
	import mx.core.Container;
	import mx.events.ChildExistenceChangedEvent;
	
	import view.Paciente;

	public class TabManager
	{
		private static var tabNav:TabNavigator;
		private static var uniqueTabs:Object = new Object();

		public static function closeAllTabs():void
		{
			tabNav.removeAllChildren();
		}

		public static function getSelected():Object
		{
			if (tabNav && tabNav.selectedChild)
				return tabNav.selectedChild;
			return null;
		}

		public static function getUniqueTab(uniqueId:String):Container
		{
			return uniqueTabs[uniqueId];
		}

		public static function openTab(tabClass:Class, uniqueId:String = null):Container
		{
			if (uniqueId && uniqueTabs[uniqueId])
			{
				tabNav.selectedChild = uniqueTabs[uniqueId];
				return uniqueTabs[uniqueId];
			}

			var tab:Container = new tabClass();

			if (uniqueId)
			{
				uniqueTabs[uniqueId] = tab;
			}

			tabNav.addChild(tab);

			tabNav.selectedChild = tab;

			return tab;
		}

		public static function removeTab(tab:Container):void
		{
			tabNav.removeChild(tab);
		}

		public static function setUniqueTab(uniqueId:String, tab:Container):void
		{
			uniqueTabs[uniqueId] = tab;
		}

		public static function set tabNavigator(tabNavigator:TabNavigator):void
		{
			tabNav = tabNavigator;
			tabNav.addEventListener(ChildExistenceChangedEvent.CHILD_REMOVE, function(event:ChildExistenceChangedEvent):void
			{
				for (var uniqueId:String in uniqueTabs)
				{
					if (uniqueTabs[uniqueId] == event.relatedObject)
					{
						uniqueTabs[uniqueId] = null;
						return;
					}
				}
			});
		}
		
		// Chamadas das telas
		public static function openPacienteView():void 
		{
			openTab(Paciente, "PacienteView");
		}
	}
}
